package com;

import javax.swing.*;
import com.trusfort.redis.DBIndex;
import com.trusfort.redis.JedisClient;
import com.trusfort.redis.RedisConnectionPool;
import com.trusfort.redis.Smember;
import com.util.FileUtils;
import com.util.Log;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import redis.clients.jedis.ScanResult;
import java.awt.*;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.awt.event.ActionEvent;

public class RedisAddRequest {
	private static JTextField txtHost;
	private static JTextField txtPort;
	private static long total = 0;
	private static JTextField txtSavePath;
	
	private static JTextArea txtLog;
	private static JTextField txtLimit;
	

	public static void logInfo(String msg) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String cur = sdf.format(new Date());

		System.out.println(msg);
		txtLog.append(msg+"\n");
		
		FileUtils.write(txtSavePath.getText() + "/logs/info.txt", cur + " [INFO] " + msg);
	}


	public void saveInvalidField(String fileName, String msg) {
		FileUtils.write(txtSavePath.getText() + "/" + fileName + ".txt", msg);
	}

	 private static String getRandomMac()
	    {
	        Random r = new Random();
	        StringBuilder sb = new StringBuilder();
	        sb.append("22");
	        for (int i=1;i<6;i++){
	            int v = r.nextInt() % 256;
	            if (v<0)
	                v = v + 256;
	            sb.append(String.format(":%02X", v));
	        }
	        return sb.toString();
	    }
	 
	 private static String getUUID32() {
		 return UUID.randomUUID().toString().replaceAll("-","");
	 }
	
	public static void main(String[] args) {
		JFrame jf = new JFrame("Redis测试数据插入");
		jf.setSize(839, 481);
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		jf.setLocationRelativeTo(null);

		// 创建内容面板，指定使用 流式布局
		JPanel panel = new JPanel();
		panel.setLayout(null);
		JButton btn05 = new JButton("批量插入");
		btn05.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				new Thread(new Runnable() {//启动数据获取，更新显示进程 
 
                public void run() { 
                	 
                	try {

						String ip = txtHost.getText();
						int port = Integer.valueOf(txtPort.getText());
						JedisClient client = new JedisClient(ip, port,null);

						logInfo("插入安卓测试数据开始>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
						
						 String KEY_PREFIX_COMMON = "XDID::";
						
						String KEY_PREFIX_DEVID = KEY_PREFIX_COMMON + "adid";
						String KEY_PREFIX_IMEI = KEY_PREFIX_COMMON + "aimei";
						String KEY_PREFIX_BTMAC = KEY_PREFIX_COMMON + "abmac";
						String KEY_PREFIX_MAC = KEY_PREFIX_COMMON + "amac";
						String KEY_PREFIX_ANDROID_ID = KEY_PREFIX_COMMON + "aaid";
						String KEY_PREFIX_BOOT_ID = KEY_PREFIX_COMMON + "abid";
						String KEY_PREFIX_BOOT_SN = KEY_PREFIX_COMMON + "absn";
						String KEY_PREFIX_FPID = KEY_PREFIX_COMMON + "afpid";
						String KEY_PREFIX_UDID = KEY_PREFIX_COMMON + "afdid";
						String KEY_PREFIX_SN = KEY_PREFIX_COMMON + "asn";
						String KEY_PREFIX_CARD_ID = KEY_PREFIX_COMMON + "acid";
						String KEY_PREFIX_INSTALL_TIME = KEY_PREFIX_COMMON + "afit";
						String KEY_PREFIX_UDID_RAND = KEY_PREFIX_COMMON + "afdidrand";
						
						
						long id = 1000l;
						
						for(int m=1;m<=100;m++) {
							List<Map<String,String>> hmsetForPipe = new ArrayList<Map<String,String>>();
							List<Smember> list = new ArrayList<Smember>();
							
							for(int i=0;i<10000;i++) {
								
								String bootId = UUID.randomUUID().toString();
								String btMac = getRandomMac();
								String mac = getRandomMac();
								String udid = getUUID32()+getUUID32();
								String fpid = getUUID32().substring(0, 16).toUpperCase();//"8A968B959CFE6040";
								String serial = getUUID32().substring(0, 16);
								String imei = getUUID32().substring(0, 16);
								String android_id = getUUID32().substring(0, 16);
								String cid = getUUID32();
								String btSn = getUUID32().substring(0, 16);
								
								String devid = String.valueOf(id);
								
								id+=1;
								
								list.add(new Smember(KEY_PREFIX_BTMAC+btMac,devid));
								list.add(new Smember(KEY_PREFIX_ANDROID_ID+android_id,devid));
								list.add(new Smember(KEY_PREFIX_BOOT_ID+bootId,devid));
								list.add(new Smember(KEY_PREFIX_BOOT_SN+btSn,devid));
								list.add(new Smember(KEY_PREFIX_FPID+fpid,devid));
								list.add(new Smember(KEY_PREFIX_SN+serial,devid));
								list.add(new Smember(KEY_PREFIX_CARD_ID+cid,devid));
								
								
								devid = getUUID32().toUpperCase();
								
								Map<String,String> map = new HashMap<String, String>();
								map.put("aid", android_id);
								map.put("bsn", btSn);
								map.put("bid", bootId);
								map.put("btmac", btMac);
								map.put("cid", cid);
								map.put("fpid", fpid);
								map.put("id",devid);
								
								hmsetForPipe.add(map);
							}
							
							client.sadd(DBIndex.XDID_DB_INDEX, list, 60*60*24);
							client.hmsetForPipe(KEY_PREFIX_DEVID, hmsetForPipe);
							
							System.out.println(">>>>>>>>>>>>"+m);
						}
						
						
//        			logInfo("  分析安卓特征值  【" + key + "】 Key数量开始");
//        				
//        				
//        			logInfo("  分析安卓特征值  【" + key + "】 Key数量结束，共" + total + "条");
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
        			
                	
                	
                } 
            }).start(); // 
			}
		});
		btn05.setBounds(717, 32, 69, 23);
		panel.add(btn05);

		jf.setContentPane(panel);

		txtHost = new JTextField();
		txtHost.setText("192.168.1.209");
		txtHost.setBounds(101, 33, 163, 21);
		panel.add(txtHost);
		txtHost.setColumns(10);

		JLabel lblIp = new JLabel("IP:");
		lblIp.setBounds(68, 36, 54, 15);
		panel.add(lblIp);

		JLabel label = new JLabel("端口：");
		label.setBounds(68, 69, 54, 15);
		panel.add(label);

		txtPort = new JTextField();
		txtPort.setText("6379");
		txtPort.setColumns(10);
		txtPort.setBounds(101, 66, 163, 21);
		panel.add(txtPort);

		JLabel label_1 = new JLabel("日志保存目录：");
		label_1.setBounds(288, 69, 96, 15);
		panel.add(label_1);

		txtSavePath = new JTextField();
		txtSavePath.setText("d:/test/");
		txtSavePath.setColumns(10);
		txtSavePath.setBounds(394, 66, 225, 21);
		panel.add(txtSavePath);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(86, 112, 700, 286);
		panel.add(scrollPane);
		
		txtLog = new JTextArea();
		txtLog.setLineWrap(true);
		scrollPane.setViewportView(txtLog);
		txtLog.setColumns(10);
		
		JLabel label_2 = new JLabel("条数大于等于：");
		label_2.setBounds(288, 36, 96, 15);
		panel.add(label_2);
		
		txtLimit = new JTextField();
		txtLimit.setText("10");
		txtLimit.setColumns(10);
		txtLimit.setBounds(394, 33, 99, 21);
		panel.add(txtLimit);
		jf.setVisible(true); // PS: 最后再设置为可显示(绘制), 所有添加的组件才会显示
	}
}